package com.wctj.api.business.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wctj.api.base.Result;
import com.wctj.api.base.entity.DdwxPaymentCfgInfo;
import com.wctj.api.base.enums.ApiCode;
import com.wctj.api.base.exception.CustomException;
import com.wctj.api.base.utils.InstanceConvertUtil;
import com.wctj.api.base.utils.JsonUtils;
import com.wctj.api.business.common.dao.DdwxPaymentCfgInfoMapper;
import com.wctj.api.business.common.dto.*;
import com.wctj.api.business.common.service.DdwxPaymentCfgInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class DdwxPaymentCfgInfoServiceImpl extends ServiceImpl<DdwxPaymentCfgInfoMapper, DdwxPaymentCfgInfo> implements DdwxPaymentCfgInfoService {
    @Override
    public DdwxPaymentCfgInfoDTO queryInfoByBizCode(String phpCallbackBizCode) {
        DdwxPaymentCfgInfo record = new LambdaQueryChainWrapper<>(baseMapper)
                .eq(DdwxPaymentCfgInfo::getBizCode, phpCallbackBizCode)
                .eq(DdwxPaymentCfgInfo::getIsDeleted, "0")
                .one();
        log.info("查询支付配置信息:{}", JsonUtils.toJson(record));
        if (Objects.isNull(record)) {
            throw new CustomException(ApiCode.PAY_CONFIG_INFO_NOT_EXISTS_FAIL);
        }
        return InstanceConvertUtil.convertObject(record, DdwxPaymentCfgInfoDTO.class);
    }

    @Override
    public DdwxPaymentCfgInfoDTO queryInfoByBizCodeAndPlatform(String phpCallbackBizCode, String systemPlatform) {
        DdwxPaymentCfgInfo record = new LambdaQueryChainWrapper<>(baseMapper)
                .eq(DdwxPaymentCfgInfo::getBizCode, phpCallbackBizCode)
                .eq(DdwxPaymentCfgInfo::getSystemPlatform, systemPlatform)
                .eq(DdwxPaymentCfgInfo::getIsDeleted, "0")
                .one();
        log.info("查询支付配置信息:{}", JsonUtils.toJson(record));
        if (Objects.isNull(record)) {
            throw new CustomException(ApiCode.PAY_CONFIG_INFO_NOT_EXISTS_FAIL);
        }
        return InstanceConvertUtil.convertObject(record, DdwxPaymentCfgInfoDTO.class);
    }

    @Override
    public Result<Object> getPlatformConfigList(PlatformConfigQueryDTO platformConfigQueryDTO) {
        //分页参数
        Page<DdwxPaymentCfgInfo> rowPage = new Page(platformConfigQueryDTO.getCurrentPage(), platformConfigQueryDTO.getPageSize());
        //queryWrapper组装查询where条件
        LambdaQueryWrapper<DdwxPaymentCfgInfo> queryWrapper = buildWrapper(platformConfigQueryDTO);
        rowPage = baseMapper.selectPage(rowPage, queryWrapper);
        List<DdwxPaymentCfgInfoDTO> ddwxPlatformConfigS = InstanceConvertUtil.convertList(rowPage.getRecords(), DdwxPaymentCfgInfoDTO.class);
        Page page = InstanceConvertUtil.convertObject(rowPage, Page.class);
        page.setRecords(ddwxPlatformConfigS);
        return Result.success(page);
    }

    @Override
    public Result<Object> deletePlatformConfig(PlatformConfigSDeleteDTO platformConfigSDeleteDTO) {
        DdwxPaymentCfgInfo ddwxPaymentCfgInfo = baseMapper.selectById(platformConfigSDeleteDTO.getId());
        if (Objects.isNull(ddwxPaymentCfgInfo)) {
            return Result.fail(ApiCode.PAY_CONFIG_INFO_NOT_EXISTS_FAIL);
        }
        ddwxPaymentCfgInfo.setIsDeleted(1);
        baseMapper.updateById(ddwxPaymentCfgInfo);
        return Result.success();
    }

    @Override
    public Result<Object> savePlatformConfig(PlatformConfigSaveDTO platformConfigSaveDTO) {
        DdwxPaymentCfgInfo ddwxPaymentCfgInfo = baseMapper.selectById(platformConfigSaveDTO.getId());
        if (Objects.isNull(ddwxPaymentCfgInfo)) {
            return Result.fail(ApiCode.PAY_CONFIG_INFO_NOT_EXISTS_FAIL);
        }
        ddwxPaymentCfgInfo = InstanceConvertUtil.convertObject(platformConfigSaveDTO, DdwxPaymentCfgInfo.class);
        baseMapper.updateById(ddwxPaymentCfgInfo);
        return Result.success();
    }

    @Override
    public Result<Object> addPlatformConfig(PlatformConfigAddDTO platformConfigAddDTO) {
        DdwxPaymentCfgInfo ddwxPaymentCfgInfo = InstanceConvertUtil.convertObject(platformConfigAddDTO, DdwxPaymentCfgInfo.class);
        baseMapper.insert(ddwxPaymentCfgInfo);
        return Result.success();
    }

    public LambdaQueryWrapper buildWrapper(PlatformConfigQueryDTO platformConfigQueryDTO) {
        LambdaQueryWrapper<DdwxPaymentCfgInfo> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(platformConfigQueryDTO.getBizCode())) {
            queryWrapper.eq(DdwxPaymentCfgInfo::getBizCode, platformConfigQueryDTO.getBizCode());
        }
        if (StringUtils.isNotBlank(platformConfigQueryDTO.getSystemPlatform())) {
            queryWrapper.eq(DdwxPaymentCfgInfo::getSystemPlatform, platformConfigQueryDTO.getSystemPlatform());
        }
        queryWrapper.eq(DdwxPaymentCfgInfo::getIsDeleted, "0");
        return queryWrapper;
    }
}
